package xxm.action;

import java.util.List;
import java.util.Vector;

import xxm.dao.IArticleDao;
import xxm.model.Article;
import xxm.model.CateInfo;
import xxm.service.IArticleManager;
import xxm.service.impl.ArticleManagerImpl;
import xxm.util.Utility;

public class ArticleAction implements AbstractAction<Article, IArticleDao> {

	private IArticleManager articleManager = new ArticleManagerImpl();
	private static ArticleAction instance = null;
	
	public static ArticleAction getInstance() {
		if (instance == null) {
			synchronized (ArticleAction.class) {
				if (instance == null) {
					instance = new ArticleAction();
				}
			}
		}
		return instance;
	}
	
	@Override
	public Object[][] getTableArray() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Vector<Vector<String>> getTableVector() {
		// TODO Auto-generated method stub
		List<Article> list = articleManager.getTableArray();
		Vector<Vector<String>> vector = new Vector<Vector<String>>();
		if( list != null ) {
			for(int i =0;i<list.size();i++){
				Vector<String> object = new Vector<String>();
				CateInfo cateInfo = CateInfoAction.getInstance().loadById(list.get(i).getCateId());
				object.add(String.valueOf(list.get(i).getId()));
				object.add(list.get(i).getTitle());
				object.add(cateInfo.getName());
				object.add(list.get(i).getOriginalSize());
				object.add(list.get(i).getCurrentSize());
				object.add(list.get(i).getShortSize());
				object.add(String.valueOf(list.get(i).getStatus()));
				object.add(list.get(i).getDate().toString());
				vector.add(object);
			}
		}
		return vector;
	}

	@Override
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		return articleManager.delete(id);
	}

	@Override
	public boolean save(Article a) {
		// TODO Auto-generated method stub
		return articleManager.save(a);
	}

	@Override
	public Article loadById(int id) {
		// TODO Auto-generated method stub
		return articleManager.loadById(id);
	}

	@Override
	public boolean update(Article a) {
		// TODO Auto-generated method stub
		return articleManager.update(a);
	}

	@Override
	public Vector<Vector<String>> getTableVectorByNameOrCode(String strKey) {
		// TODO Auto-generated method stub
		List<Article> list = null;
		if (Utility.isNum(strKey)) {
			list = articleManager.getTableVectorByCode(strKey);
		}else {
			list = articleManager.getTableVectorByName(strKey);
		}
		Vector<Vector<String>> vector = new Vector<Vector<String>>();
		if( list != null ) {
			for(int i =0;i<list.size();i++){
				Vector<String> object = new Vector<String>();
				CateInfo cateInfo = CateInfoAction.getInstance().loadById(list.get(i).getCateId());
				object.add(String.valueOf(list.get(i).getId()));
				object.add(list.get(i).getTitle());
				object.add(cateInfo.getName());
				object.add(list.get(i).getOriginalSize());
				object.add(list.get(i).getCurrentSize());
				object.add(list.get(i).getShortSize());
				object.add(String.valueOf(list.get(i).getStatus()));
				object.add(list.get(i).getDate().toString());
				vector.add(object);
			}
		}
		return vector;
	}

}
